68. Security and Cryptography

Note

The below information is extensively based in information taken from the PowerShell® Notes for Professionals book. I plan to extend this information based on my day to day usage of the language.

68.1: Calculating a string's hash codes via .Net Cryptography

Utilizing .Net System.Security.Cryptography.HashAlgorithm namespace to generate the message hash code with the algorithms supported.

1
2
3
4
5
6
7
$example="Nobody expects the Spanish Inquisition."

#calculate
$hash=[System.Security.Cryptography.HashAlgorithm]::Create("sha256").ComputeHash([System.Text.Encoding]::UTF8.GetBytes($example))

#convert to hex
[System.BitConverter]::ToString($hash)
1
#2E-DF-DA-DA-56-52-5B-12-90-FF-16-FB-17-44-CF-B4-82-DD-29-14-FF-BC-B6-49-79-0C-0E-58-9E-46-2D-3D

The "sha256" part was the hash algorithm used. The - can be removed or change to lower case

1
2
#convert to lower case hex without '-'
[System.BitConverter]::ToString($hash).Replace("-","").ToLower()
1
#2edfdada56525b1290ff16fb1744cfb482dd2914ffbcb649790c0e589e462d3d

If base64 format was preferred, using base64 converter for output

1
2
#convert to base64
[Convert]::ToBase64String($hash)
1
#Lt/a2lZSWxKQ/xb7F0TPtILdKRT/vLZJeQwOWJ5GLT0=